package cn.hengzhu.main_manager.utils.filter;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import cn.hengzhu.main_manager.repository.dao.ManageCaseMapper;
import cn.hengzhu.main_manager.repository.domain.ManageCase;
import cn.hengzhu.main_manager.repository.domain.ManageCaseExample;
import cn.hengzhu.main_manager.repository.vo.CaseInfoPutRedisVO;
import cn.hengzhu.main_manager.utils.AliMsmUtils;
import cn.hengzhu.main_manager.utils.RedisUtil;

/**
 * reidsKey过期通知
 * @param message
 */
public class RedisKeyExpirationListener {
	

	@Autowired
	private RedisUtil redisUtil;
	
	@Autowired
	private ManageCaseMapper manageCaseMapper;
	
	public void handleMessage(String message) {
		if(message!=null){
			String caseNoKey = message.replace(RedisUtil.ON_LINE_BOX,"");
			String dataBaseOnlineKey = RedisUtil.DATA_BASE_BOX_ON_LINE+caseNoKey;
			CaseInfoPutRedisVO getRedisCase = (CaseInfoPutRedisVO)redisUtil.get(dataBaseOnlineKey);
			
			//查询该储物柜信息
			ManageCaseExample manageCaseExample= new ManageCaseExample();
			manageCaseExample.createCriteria().andCaseNoEqualTo(getRedisCase.getCaseNo());
			List<ManageCase> mcList = manageCaseMapper.selectByExample(manageCaseExample);
			//将数据库缓存中此key改为不在线
			redisUtil.renameKey(dataBaseOnlineKey, RedisUtil.DATA_BASE_BOX_NOT_LINE+caseNoKey);
			ManageCase manageCase = new ManageCase();
			manageCase.setIsOnline((byte)0);
			manageCaseMapper.updateByExampleSelective(manageCase,manageCaseExample);
			
			//短信通知
			AliMsmUtils.boxEventNotify(mcList,"离线了");
		}
	}
	
}
